﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config
{
    [ConfigurationCollection(typeof(DesktopContactElement),
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
    public class DesktopContactElementCollection : ConfigurationElementCollection
    {
        static DesktopContactElementCollection()
        {
            properties = new ConfigurationPropertyCollection();
        }

        public DesktopContactElementCollection() { }

        private static ConfigurationPropertyCollection properties;

        protected override ConfigurationPropertyCollection Properties
        {
            get { return properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        public DesktopContactElement this[int index]
        {
            get { return (DesktopContactElement)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public DesktopContactElement GetByContactId(int contactId)
        {
            foreach (DesktopContactElement contactElement in this)
            {
                if (contactElement.ContactId == contactId)
                {
                    return contactElement;
                }
            }

            return null;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new DesktopContactElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as DesktopContactElement).ContactId;
        }

        public void Add(DesktopContactElement contactElement)
        {
            base.BaseAdd(contactElement);
        }

        public void Remove(string sipUri)
        {
            base.BaseRemove(sipUri);
        }

        public void Remove(int contactId)
        {
            base.BaseRemove(contactId);
        }

        public void Remove(DesktopContactElement contactElement)
        {
            base.BaseRemove(GetElementKey(contactElement));
        }

        public void Clear()
        {
            base.BaseClear();
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }

        public string GetKey(int index)
        {
            return (string)base.BaseGetKey(index);
        }
    }
}
